xlv.util={
	matchAttributes:function(element,attributes){
		var e=element;
		if(!e||e.nodeType!=1)return false;
		if(!attributes)return true;
		return $H(attributes).all(function(p,index){
			if('tag'==p.key||'tagName'==p.key) return xlv.util.isTag(e,p.value);
			if('object'==p.key)	return e==p.value;
			if(!Element.hasAttribute(e,p.key))return false;
			if('*'==p.value)return true;
			var v=Element.readAttribute(e,p.key);
			return (p.value instanceof RegExp) ? p.value.test(v) : p.value==v;
		});
	},
	isTag:function(element,tag){
		return (element && element.nodeType==1 && element.tagName.toUpperCase()==tag.toUpperCase());
	},
	findChild:function(element,attributes,deep_first,without){
		var child;
		if(!element||element.nodeType!= 1||!(child=element.firstChild))return null;
		without=(typeof without=='undefined' && !attributes.xlvobject)?{xlvobject:'*'}:without;
		if(deep_first){
			do{
				if(xlv.util.matchAttributes(child,attributes)){
					if(without && xlv.util.matchAttributes(child,without))continue;
					return child;
				}
				var children=xlv.util.findChild(child,attributes,true,without);
				if(children)return children;
			}while(child=child.nextSibling);
			return null;
		}
		var siblings=[];
		do{
			if(xlv.util.matchAttributes(child,attributes)){
				if(without && xlv.util.matchAttributes(child,without))continue;
				return child;
			}
			if(child.nodeType==1)siblings.push(child);
		}while(child=child.nextSibling);
		while(child=siblings.shift()){
			if(without && xlv.util.matchAttributes(child,without))continue;
			if(child=xlv.util.findChild(child,attributes,false,without))return child;
		}
		return null;
	},
	findDirectChild:function(element,attributes,without){
		var child;
		if(!element||element.nodeType!= 1||!(child=element.firstChild))return null;
		without=(typeof without=='undefined'&& !attributes.xlvobject)?{xlvobject:'*'}:without;
		do{
			if(xlv.util.matchAttributes(child,attributes)){
				if(without && xlv.util.matchAttributes(child,without))continue;
				return child;
			}
		}while(child=child.nextSibling);
		return null;
	},
	children:function(element,attributes,without){
		var nodes=[],got=[];
		if(!element||element.nodeType!=1||!(nodes=element.childNodes))return got;
		without=(typeof without=='undefined'&& !attributes.xlvobject)?{xlvobject:'*'}:without;
		for(var i=0,n=nodes.length;i<n;++i){
			if(xlv.util.matchAttributes(nodes[i],attributes)){
				if(without && xlv.util.matchAttributes(nodes[i],without))continue;
				got.push(nodes[i]);
			}
		}
		if(got.length>0)return got;
		for(var i=0,n=nodes.length;i<n;++i){
			if(without && xlv.util.matchAttributes(nodes[i],without))continue;
			got=xlv.util.children(nodes[i],attributes,without);
			if(got.length > 0)return got;
		}
		return got;
	},
	descendants:function(element,attributes,one){
		var got=[];
		if(!element||element.nodeType!= 1||!element.hasChildNodes())return (one) ? null:got;
		var tag=attributes['tag']||attributes['tagName']||null; 
		var nodes=element.getElementsByTagName((tag)?tag:'*');
		for(var i=0,n=nodes.length;i<n;++i){
			if(xlv.util.matchAttributes(nodes[i],attributes)) {
				if(one)return nodes[i];
				got.push(nodes[i]);
			}
		}
		return got;
	},
	findDescendant:function(element,attributes){
		return xlv.util.descendants(element,attributes,true);
	},
	siblings:function(element,attributes){
		var got=[],self=element;
		if(!element) return got;
		while(element=xlv.util.findPrevSibling(element,attributes))got.unshift(element);	
		element=self;	
		while(element=xlv.util.findNextSibling(element,attributes))got.push(element);	
		return got;
	},
	findNextSibling:function(element,attributes){
		while(element&&(element=element.nextSibling)){
			if(xlv.util.matchAttributes(element,attributes))return element;
		}
		return null;
	},
	findPrevSibling:function(element,attributes){
		while(element&&(element=element.previousSibling))
			if(xlv.util.matchAttributes(element,attributes))return element;
		return null;
	},
	previousSiblings:function(element,attributes){
		var got=[];
		if(!element) return got;
		while(element=xlv.util.findPrevSibling(element,attributes))got.unshift(element);	
		return got;
	},
	nextSiblings:function(element,attributes){
		var got=[];
		if(!element) return got;
		while(element=xlv.util.findNextSibling(element,attributes))got.push(element);	
		return got;
	},
	ancestors:function(element,attributes,boundary){
		var got=[];
		while(element=xlv.util.findParent(element,attributes,boundary))got.push(element);
		return got;
	},
	findParent:function(element,attributes,boundary){
		if(!element||!(element=element.parentNode)) return null;
		do {
			if(boundary && xlv.util.matchAttributes(element,boundary))return null;
			if(xlv.util.matchAttributes(element,attributes))return element;
		}while(element=element.parentNode);
		return null;
	},
	levelOfDescendant:function(parent,element,attributes){
		var level=0;
		while(element&& element!=parent){
			level += xlv.util.matchAttributes(element,attributes)?1:0;
			element=element.parentNode;
		}
		return level;
	},
	isDescendantOf:function(element,parent){
		try{
			while(element && element!=parent){
				if(element==document.body){
					if(parent!=element) element=null;
					break;
				}
				element=element.parentNode;
			}
			return element ? true : false;
		}catch(e){}
		return false;
	},
	getObjects:function(element,object_name){
		var comps=[],name;
		var nodes=element.childNodes;
		for(var i=0,n=nodes.length;i<n;++i){
			if(nodes[i].nodeType==1) {
				if(name=nodes[i].getAttribute('xlvobject')){
					if(!object_name||object_name==name) comps.push(nodes[i]);
				}else comps=comps.concat(xlv.util.getObjects(nodes[i],object_name));
			}
		}
		return comps;
	},
	forceRerendering:function(element) {
		try {
		   var n = document.createTextNode(' ');
		   element.appendChild(n);
		   element.removeChild(n);
		}catch(e){}
	},
	inspect:function(element){
		var s='';
		while(element){
			if(element.nodeType==1) {
				s+='<'+element.tagName;
				if(element.id) s+=' id='+element.id+' ';
				if(element.getAttribute('xlvobject')) s+= 'xlvobject='+element.getAttribute('xlvobject');
				s+='>';
			}
			element=element.parentNode;
		}
		return s;
	},
	isDisplayNone:function(element){
		while(element){
			if(element.nodeType==1 && Element.getStyle(element,'display')=='none')return true;
			element=element.parentNode;
		}
		return false;
	},
	locateImplement:function(element,implement){
		if(!element) return null;
		do {
			if(element['isImplement'] && element.isImplement(implement))return element;
			if(element==document.body)break;
		}while(element=element.parentNode);
		return null;
	},
	bindAsEventListener:function(object,function_name){
		var __method=object[function_name],args=$A(arguments), object=args.shift();
		return function(event){
			if(!object['_$observeFlag'])return false;
			return __method.apply(object,[(event||window.event)].concat(args).concat($A(arguments)));
		};
	},
	styleEffect:function(options){
		Object.implement(options,{
			element:null,
			style:{},
			properties:null,
			duration:((Prototype.Browser.IE)?500:350),
			transition:'bezier',
			bezierName:'ease',
			pulse:5,
			cancel:false,
			setup:null,
			processed:null,
			finished:null,
			canceled:null,
			completed:false,
			_startStyle:null,
			_deltaStyle:null,
			_startProperties:null,
			_deltaProperties:null,
			_startTime:null,
			__chain:null
			});
		if(options.cancel){
			if(options.canceled)options.canceled();
			options.completed=true;
			return;
		}
		if(options.setup&&!options._setup){
			options._setup=true;
			options.setup();
		}
		if(options._startTime===null)options._startTime=new Date().getTime();
		var s;
		if(Prototype.Browser.MobileSafari && !options.properties){
			var trans={'-webkit-transition':'all 0.35s ease'};
			Element.setStyle(options.element, trans);
			var end = (function(){
				options.element.removeEventListener('webkitTransitionEnd', end, false);
				if(options.finished)options.finished();
				options.completed=true;
				if(options.__chain)setTimeout(function(){options.__chain();}, 2);
				}).bindAsEventListener(options.element);
			options.element.addEventListener('webkitTransitionEnd', end, false);
			for(s in options.style)	{
				if(s=='opacity')Element.setStyle(options.element,{opacity:cs});
				else options.element.style[s]=parseFloat(options.style[s]||0)+'px';
			}
			return;
		}
		if(options._startStyle===null){
			options._startStyle={};
			options._deltaStyle={};
			for(s in options.style)	{
				options._startStyle[s]=parseFloat(Element.getStyle(options.element,s)||0);
				options._deltaStyle[s]=parseFloat(options.style[s]||0)-options._startStyle[s];
			}
		}
		if(options.properties && options._startProperties===null){
			options._startProperties={};
			options._deltaProperties={};
			for(s in options.properties)	{
				options._startProperties[s]=parseFloat(options.element[s]||0);
				options._deltaProperties[s]=parseFloat(options.properties[s]||0)-options._startProperties[s];
			}
		}
		var current=new Date().getTime();
		var p=(current-options._startTime)/options.duration;
		p=(p>1)?1:p;
		var p2=1;
		if(options.transition=='bezier'){
			p2=xlv.util.bezier(options.bezierName)(p);
		}else if(options.transition=='sinoidal'){
			p2=(-Math.cos(p*Math.PI)/2)+0.5;
		}else if(options.transition=='reverse'){
			p2=1-p;
		}else if(options.transition=='flicker'){
			p2=((-Math.cos(p*Math.PI)/4)+0.75) + Math.random()/4;
			p2 = p2>1?1:p2;
		}else if(options.transition=='webble'){
			p2=(-Math.cos(p*Math.PI*(9*p))/2)+0.5;
		}else if(options.transition=='pulse'){
			var pulse=options.pulse||5;
			p2=((p%(1/pulse))*pulse).round() == 0 ?
					((p*pulse*2)-(p*pulse*2).floor()) :
				1 - ((p*pulse*2)-(p*pulse*2).floor());
		}
		for(s in options.style){
			var cs=options._deltaStyle[s]*p2+options._startStyle[s];
			if(s=='opacity')Element.setStyle(options.element,{opacity:cs});
			else options.element.style[s]=cs.round()+'px';
		}
		if(options.properties){
			for(s in options.properties){
				options.element[s]=options._deltaProperties[s]*p2+options._startProperties[s];
			}
		}
		if(options.processed) options.processed();
		if(p<1){
			var sleep=(40-(new Date().getTime()-current));
			sleep = (sleep<0)?2:sleep;
			setTimeout(function(){xlv.util.styleEffect(options);},sleep);
		}else if(options.finished||options.__chain){
				if(options.finished)options.finished();
				options.completed=true;
				if(options.__chain)setTimeout(function(){options.__chain();}, 2);
		}
	},
	__styleEffectQueue:[],
	__currentStyleEffectOptions:null,
	queueStyleEffect:function(options){
		if(options){
			options.__chain=function(){xlv.util.queueStyleEffect(null);};
			xlv.util.__styleEffectQueue.push(options);
			if(xlv.util.__currentStyleEffectOptions==null)options=null;
		}
		if(options==null){
			var next=xlv.util.__styleEffectQueue.shift();
			xlv.util.__currentStyleEffectOptions=next;
			if(next)setTimeout(function(){xlv.util.styleEffect(next);},2);
		}
	},
	_autoIncInteger:1,
	autoIncInteger:function(){
		return xlv.util._autoIncInteger++;
	},
	addComma:function(str){
		str+= '';
		var x = str.split('.');
		var x1 = x[0];
		var x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	},
	getCookie:function(name){
		var cookies=document.cookie+';';
		var pos=cookies.indexOf(name+'=');
		if(pos!=-1){
			pos+=(name.length)+1;
			return cookies.substring(pos,cookies.indexOf(";",pos));
		}
		return null;
	},
	removeCookie:function(name){
		var value=xlv.util.getCookie(name);
		//document.cookie=name+'='+value+';max-age=0;path=/xlive;';
		document.cookie=name+'='+value+';max-age=0;path=/;';
	},
	hash:function(data){
		return MD5.hex_md5(data);	    
	},
	ulliPath:function(li){
		if(!li) return'';
		var p=xlv.util.findParent(li,{tag:'ul'},{xlvobject:'*'});
		if(p)p=xlv.util.findParent(p,{tag:'li'},{xlvobject:'*'});
		var c=0;
		do{
			++c;
		}while(li=li.previousSibling);
		return xlv.util.ulliPath(p)+'/*['+c+']';
	},
	inspectObject:function(obj){
		var msg='';
		var i;
		for(i in obj){
			msg+= i +':'+obj[i]+'\n';
		}
		return msg;
	},
	dmSearch:function(element,dm_name){
		if(!element)return null;
		var dmc=xlv.util.locateImplement(element,'xlv.imp.dmc');
		if(dmc){
			var dm=dmc.dmc_dmLookup(dm_name);
			if(dm)return dm;
			if(dmc!=xlv.desktop) return xlv.util.dmSearch(dmc.parentNode,dm_name);
		}
		return null;
	},
	getElementValue:function(element){
		var value='';
		var nodes=element.childNodes;
		for(var i=0,n=nodes.length;i<n;++i)
			value += (nodes[i].nodeType==3)?nodes[i].nodeValue :'';
		return value;
	},
	removeElementValue:function(element){
		var nodes=element.childNodes;
		for(var i=nodes.length-1;i>=0;--i)
			if(nodes[i].nodeType==3) element.removeChild(nodes[i]);
	},
	setElementValue:function(element,value){
		try{
			if(!element||value==null) return;
			if(element.nodeType==3) element.nodeValue=value;
			else if(element.nodeType==1){
				var nodes=element.childNodes;
				var text_node=null;
				for(var i=nodes.length-1;i>=0;--i){
					if(nodes[i].nodeType==3) {
						if(text_node)element.removeChild(text_node);
						text_node=nodes[i];
					}
				}
				if(text_node)text_node.nodeValue=value;
				else element.insertBefore(element.ownerDocument.createTextNode(value),element.firstChild);
			}
		}catch(e){alert('xml_setNodeValue :'+e);}
	},
	methodNodeToQueryString:function(node,path){
		var name=node.nodeName;
		var qstr =(path)?'&'+name+'='+ encodeURIComponent(xlv.util.getXmlText(node)):'method='+name;
		path+=(path)?'.'+name:name;
		for(var i=0,n=node.childNodes.length;i<n;++i){
			if(node.childNodes[i].nodeType==1) qstr+=xlv.util.methodNodeToQueryString(node.childNodes[i],path);
		}
		return qstr;
	},
	indexesToXPath:function(indexes){
		var xpath=indexes.replace(/\./g,']/*[');
		return xpath=='' ? '.':'*['+xpath+']';
	},
	indexesForLI:function(li){
		var indexes='';
		do{
			if(li){
				var prev=xlv.util.previousSiblings(li,{tag:'li'});
				indexes=(prev.length+1)+((indexes=='')? '':'.')+indexes;
			}
		}while(li=xlv.util.findParent(li,{tag:'li'},{xlvobject:'*'}));
		return indexes;
	},
	findLIAtIndexes:function(root_ul,indexes){
		indexes=indexes.split(/\./);
		var ul=root_ul,li;
		for(var i=0;i<indexes.length;++i){
			li=null;
			if(ul && ul.hasChildNodes()){
				for(var c=0,count=0;c<ul.childNodes.length;++c){
					if(xlv.util.isTag(ul.childNodes[c],'li'))++count;
					if(count==indexes[i]) {li=ul.childNodes[c];break;}
				}
			}
			if(!li) break;
			ul=xlv.util.findChild(li,{tag:'ul'},{xlvobject:'*'});
		}
		return li;
	},
	hasElementChildNode:function(node){
		if(node&&node.nodeType==1){
			var children=node.childNodes;
			for(var i=0,n=children.length;i<n;++i)
				if(children[i].nodeType==1) return true;
		}
		return false;
	},
	isLeafLI:function(li){
		return xlv.util.findChild(li, {tag:'ul'},{object:'*'})==null ;
	},
	isLeafAtIndexes:function(root_ul,indexes){
		var li=xlv.util.findLIAtIndexes(root_ul,indexes);
		return (li)?xlv.util.isLeafLI(li):false;
	},
	getEditorPolicy:function(obj_name){
		var obj=xlv.obj[obj_name];
		return (obj&&obj['method']['editor_policy'])?obj['method']['editor_policy']():{};
	},
	selectXmlSingleNode:function(xpath,node){
		return (node)?xlv.imp.xml.method.xml_selectSingleNode(xpath,node):null;
	},
	selectXmlNodes:function(xpath,node){
		return (node)?xlv.imp.xml.method.xml_selectNodes(xpath,node):null;
	},
	getXmlNodeValue:function(xpath,node){
		var vnode=(node)?xlv.imp.xml.method.xml_selectSingleNode(xpath,node):null;
		return (vnode)? xlv.imp.xml.method.xml_getNodeValue(vnode):null;
	},
	setXmlNodeValue:function(xpath,node,value){
		var node=(node)?xlv.util.selectXmlSingleNode(xpath,node):null;
		if(node)xlv.imp.xml.method.xml_setNodeValue(node,value);
	},
	getXmlText:function(node){
		return node?(Prototype.Browser.IE)?node.text:node.textContent : '';
	},
	setXmlText:function(node,text){
		if(Prototype.Browser.IE)node.text=text;
		else node.textContent=text;
	},
	xmlExtendNodes:function(target,source,operator,clone,deep,ext_empty,exclude_names){
		clone=(clone===false)?false:true;
		ext_empty=(ext_empty===true)?true:false;
		deep=(deep||9999);
		if(!target||!source)return;
		if(!source.hasChildNodes())return;
		var children=[];
		for(var s=0;s<source.childNodes.length;++s) children.push(source.childNodes[s]);
		var processed=[];
		for(var i=0;i<children.length;++i){
			var item=children[i];
			if(!item)break;
			var name=item.nodeName;
			var same=xlv.util._xmlFindSameChild(target,item,processed);
			if(same){
				processed.push(same);
				if(item.nodeType == 3/*TEXT_NODE*/ || item.nodeType==4/*CDATA*/||item.nodeType==8/*comment*/){
					if('overwrite'==operator)xlv.util.setXmlText(same,xlv.util.getXmlText(item));
					else if("extend"==operator){
							if(ext_empty && xlv.util.getXmlText(same).strip().length()==0)
								xlv.util.setXmlText(same,xlv.util.getXmlText(item));
						 }
					else if('append'==operator){
							if(xlv.util.getXmlText(same).strip()!=xlv.util.getXmlText(item).strip()){
								var new_text=item.cloneNode(true);
								var new_text_parent=item.parentNode.cloneNode(false);
								new_text_parent.appendChild(new_text);
								same.parentNode.parentNode.appendChild(new_text_parent);
							}
						}
					else if('remove'==operator){
							var same_parent = same.parentNode;
							same_parent.removeChild(same);
							while(same_parent && !same_parent.hasChildNodes()) {
								var node=same_parent;
								same_parent=same_parent.parentNode;
								same_parent.removeChild(node);
							}
							continue;
						  }
				}else if(!item.hasChildNodes()){
					if("remove"==operator){
						var same_parent = same.parentNode;
						same_parent.removeChild(same);
						while(same_parent && !same_parent.hasChildNodes()) {
							var node=same_parent;
							same_parent=same_parent.parentNode;
							same_parent.removeChild(node);
						}
					}
					if("append"==operator){
						var new_node=(clone)?item.cloneNode(true):item;
						if(same.ownerDocument.adoptNode) new_node=same.ownerDocument.adoptNode(new_node);
						same.parentNode.appendChild(new_node);
					}
					if("overwrite"==operator){
						xlv.util.setXmlText(same,xlv.util.getXmlText(item));
					}
					continue;
				}
				if('overwrite'==operator||'extend'==operator) xlv.util.xmlExtendAttributes(same,item,operator,ext_empty);
				if(deep>0)xlv.util.xmlExtendNodes(same,item,operator,clone,deep-1,ext_empty);
			}else {
				if(deep>0){
					if('overwrite'==operator||'extend'==operator||'append'==operator) {
						var new_node=(clone)?item.cloneNode(true) : item;
						if(target.ownerDocument.adoptNode) new_node=target.ownerDocument.adoptNode(new_node);
						target.appendChild(new_node);
						processed.push(new_node);
					}
				}
			}
		}
	},
	_xmlFindSameChild:function(target,child,processed){
		if(!target.hasChildNodes())return null;
		var name=child.nodeName;
		var type=child.nodeType;
		for(var i=0,n=target.childNodes.length;i<n;++i){
			var item=target.childNodes[i];
			if(processed.include(item)) continue;
			if(name==item.nodeName && type==item.nodeType) return item;
		}
		return null;
	},
	xmlExtendAttributes:function(target,source,operator,ext_empty){
		if(!target||!source||target.nodeType!=1||source.nodeType!= 1) return;
		var map=source.attributes;
		for(var i=0,n=map.length;i<n;++i){
			var name=map[i].name;
			if("overwrite"==operator)target.setAttribute(name,map[i].value);
			else if("extend"==operator){
					if(!ext_empty && target.getAttributeNode(name)!=null)continue;
					var value=target.getAttribute(name);
					if(value!=null || value=='')target.setAttribute(name,map[i].value);
				}
		}
	}
};

(function() {
	  var E = xlv.util.bezier=function(x1,y1,x2,y2) {
	    if (arguments.length == 1) {return E[x1] || E['ease'];}
	    var cx = 3 * x1;
	    var bx = 3 * (x2 - x1) - cx;
	    var ax = 1 - cx - bx;
	    var cy = 3 * y1;
	    var by = 3 * (y2 - y1) - cy;
	    var ay = 1 - cy - by;
	    var T = function(t) {
	    	var t0 = 0, t1 = 1, t2 = t, x2, d2, i;
	    	for(i = 0; i < 8; i++) {
	    		x2 = (((ax * t2 + bx) * t2 + cx) * t2) - t;
	    		if (-0.000001 < x2 && x2 < 0.000001)return t2;
	    		d2 = (3 * ax * t2 + 2 * bx) * t2 + cx;
	    		if (-1e-6 < d2 && d2 < 1e-6)break;
	    		t2 = t2 - x2 / d2;
	    	}
	    	t2 = t;
	    	while (t0 < t1) {
	    		x2 = ((ay * t2 + by) * t2 + cy) * t2;
	    		i = x2 - t;
	    		if(-0.000001 < i && i < 0.000001)return t2; //?
	    		if (t > x2) t0 = t2;
	    		else t1 = t2;
	    		t2 = (t1 - t0) * 0.5 + t0;
	    	}
	    	return t2;
	    };
	    return function(t) {
	      if (t <= 0) return 0;
	      else if (t >= 1) return 1;
	      t = T(t);
	      return ((ay * t + by) * t + cy) * t;
	    };
	  };
	  E['linear'] = function(t) { return t; };
	  E['ease-in'] = E(0.42,0, 1,1);
	  E['ease-out'] = E(0,0, 0.58,1);
	  E['ease-in-out'] = E(0.42,0,0.58,1);
	  E['ease-out-in'] = E(0.0,0.42,1.0,0.58);
	  E['ease'] = E(0.25,0.1, 0.25,1);
	})();


























